<?php

function getPage($obj,$table,$page,$pagesize,$pagenum){
            $arr=array();     

			//获得数据总条数
			$sql="select count(*) as total from `{$table}`"; 
			$total=$obj->getOne($sql);
            
			//总数除以每页要显示的条数等于所有的页码数，并且向上取值（如2.5，取值3）
			$all=ceil($total['total']/$pagesize);


			if($page<0){
			   $page=1;
			}
			if($page>$all){
			   $page=$all;
			}

			//判断页码是不是数字或者字符串
			if(!is_numeric($page)){
			    $page=1;
			}

			$offset=($page-1)*$pagesize;//设置偏移量
			$sql2="select a.*,b.name from `news` a inner join `cate` b on a.cid=b.id order by id desc limit {$offset},{$pagesize}";

            $arr['content']=$obj->getCondition($sql2);

			$other=floor($pagenum/2);
			$start=$page-$other;
			$end=$page+$other;

             
			if($start<=0){
			   $start=1;
			   $end=$pagenum;
			}

			if($end>$all){
				$end=$all;
				// $start=$all-$start+1;
			}




			$pagestring='';
			$prev=$page-1;
			$prevshow=($page==1)?'disabled':'';


		    $pagestring.='<li class="'.$prevshow.'"><a href="index.php?mod=admin&c=news&a=list&page=1" id="datatable1_first" tabindex="0" class="paginate_button first">首页</a></li>';

			$pagestring.='<li class="'.$prevshow.'"><a href="index.php?mod=admin&c=news&a=list&page='.$prev.'" id="datatable1_previous" tabindex="0" class="paginate_button previous">上一页</a></li>';




			for ($i=$start;$i<=$end;$i++) {

				 $active=$page==$i?'active':'';
			     $pagestring.='	<li class="'.$active.'">
				 <a href="index.php?mod=admin&c=news&a=list&page='.$i.'" tabindex="0">'.$i.'</a>
				 </li>';
			}





			$next=$page+1;
			$nextshow=($page==$all)?'disabled':'';
			$pagestring.='<li class="'.$nextshow.'"><a href="index.php?mod=admin&c=news&a=list&page='.$next.'" id="datatable1_next" tabindex="0" class="paginate_button next">下一页</a></li>';

			$pagestring.='<li class="'.$nextshow.'"><a href="index.php?mod=admin&c=news&a=list&page='.$all.'" id="datatable1_last" tabindex="0" class="paginate_button last">尾页</a></li>';


	        $arr['pagestring']=$pagestring;
            return $arr;
}




